package de.rj.urwalking.ui.fragments.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import de.rj.urwalking.R;
import de.rj.urwalking.config.Constants;
import de.rj.urwalking.data.SalientObject;
import de.rj.urwalking.ui.activities.URwalkingActivity;
import de.rj.urwalking.util.ExperimentManager;

/**
 * 
 * @author rjj
 *
 */
public abstract class LandmarkDialogFragment extends DialogFragment implements OnClickListener {
	

	private Button backButton;
	private Button suggestButton;
	protected TextView infoTextView;

	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.landmark_detail, container);

		infoTextView = (TextView) view.findViewById(R.id.infoTextView);
		suggestButton = (Button) view.findViewById(R.id.suggestButton);
		suggestButton.setOnClickListener(this);
		backButton = (Button) view.findViewById(R.id.backButton);
		backButton.setOnClickListener(this);

		
		Window window = getDialog().getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.BOTTOM;
		wlp.dimAmount = 0.0f;
		window.setAttributes(wlp);		
		window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		
		return view;
	}

	protected void startQuestions(Intent intent) {

		intent.putExtra("projectionMatrix", getArguments().getSerializable("projectionMatrix"));				
		intent.putExtra("currentLatitude", getArguments().getDouble("currentLatitude"));				
		intent.putExtra("currentLongitude", getArguments().getDouble("currentLongitude"));				
		getActivity().startActivityForResult(intent, Constants.QUESTIONS_ACTIVITY_REQUEST_CODE);
	}

	protected void ensureExperiment() {
		if (!ExperimentManager.getInstance().isExperimentRunning()) {
			((URwalkingActivity) getActivity()).startExperiment();
		}
	}
	
	protected void showMoreInfo(SalientObject salientObject) {
		showPreviousNames(salientObject);
	}

	protected void showPreviousNames(SalientObject salientObject) {

		if (salientObject.getAllNames().size() > 0) {

			infoTextView.setText("Bereits bezeichnet als:\n");

			for (String name : salientObject.getAllNames()) {
				infoTextView.append(name + "\n");
			}
		}
		else {
			hideInfoView();
		}

	}	

	protected void hideInfoView() {
		infoTextView.setVisibility(View.GONE);
	}

}
